/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package players;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import weapons.BurningArrow;
import weapons.SimpleArrow;
import weapons.Weapon;

/**
 *
 * @author RoKe
 */
public class Players {

    private String pl = "PlayerRed.gif";
    private String pr = "PlayerYellowRight.gif";
    private int dx;
    private int dy;
    private int x;
    private int y;
    private int width = 22;
    private int height = 38;
    private int board_width = 480;
    private Image image;
    private List<Weapon> projectiles;
    private final int BOWMAN_SIZE = 20;
    private int controles;
    private final int maxHealth = 100;
    private int health = maxHealth;
    private int selected_weapon_left = 1;
    private int selected_weapon_right = 1;

    public Players(int x, int y, int controles) {
        if (controles == 0) {
            ImageIcon ii = new ImageIcon(this.getClass().getResource(pl));
            image = ii.getImage().getScaledInstance(width, height, 1);
        }
        if (controles == 1) {
            ImageIcon ii = new ImageIcon(this.getClass().getResource(pr));
            image = ii.getImage().getScaledInstance(width, height, 1);
        }
        projectiles = new ArrayList<>();

        this.x = x;
        this.y = y;
        this.controles = controles;
    }

    public void move() {

        x += dx;
        y += dy;
        if (x > 480 - width) {
            x = 480 - width;
        }
        if (x < 0) {
            x = 0;
        }
        if (y > 300 - height) {
            y = 300 - height;
        }
        if (y < 0) {
            y = 0;
        }
        if (controles == 0) {
            if (x > 480 / 2 - width) {
                x = 480 / 2 - width;
            }
        }
        if (controles == 1) {
            if (x < 480 / 2) {
                x = 480 / 2;
            }
        }

    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Image getImage() {
        return image;
    }

    public List<Weapon> getProjectiles() {
        return projectiles;
    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
        if (controles == 0) {
            if (key == KeyEvent.VK_F) {
                fire();
            }
            if (key == KeyEvent.VK_Q) {
                if (selected_weapon_left == 1) {
                    selected_weapon_left = 2;
                } else {
                    selected_weapon_left--;
                }
            }
            if (key == KeyEvent.VK_E) {
                if (selected_weapon_left == 2) {
                    selected_weapon_left = 1;
                } else {
                    selected_weapon_left++;
                }
            }

            if (key == KeyEvent.VK_A) {
                dx = -1;
            }

            if (key == KeyEvent.VK_D) {
                dx = 1;
            }
            if (key == KeyEvent.VK_W) {
                dy = -1;
            }

            if (key == KeyEvent.VK_S) {
                dy = 1;
            }

        }
        if (controles == 1) {
            if (key == KeyEvent.VK_J) {
                fire();
            }
            if (key == KeyEvent.VK_K) {
                if (selected_weapon_left == 1) {
                    selected_weapon_left = 2;
                } else {
                    selected_weapon_left--;
                }
            }
            if (key == KeyEvent.VK_L) {
                if (selected_weapon_left == 2) {
                    selected_weapon_left = 1;
                } else {
                    selected_weapon_left++;
                }
            }

            if (key == KeyEvent.VK_LEFT) {
                dx = -1;
            }

            if (key == KeyEvent.VK_RIGHT) {
                dx = 1;
            }
            if (key == KeyEvent.VK_UP) {
                dy = -1;
            }

            if (key == KeyEvent.VK_DOWN) {
                dy = 1;
            }
        }
    }

    public void fire() {
        int counterSimpleArrow = 0;
        int counterBurningArrow = 0;
        for (int i = 0; i < projectiles.size(); i++) {
            if (projectiles.get(i) instanceof SimpleArrow) {
                counterSimpleArrow++;
            }
            if (projectiles.get(i) instanceof BurningArrow) {
                counterBurningArrow++;
            }
        }
        if (counterSimpleArrow <= 3 && selected_weapon_left == 1) {
            projectiles.add(new SimpleArrow(x + BOWMAN_SIZE, y + BOWMAN_SIZE / 2, controles));
        }
        if (counterBurningArrow == 0 && selected_weapon_left == 2) {
            projectiles.add(new BurningArrow(x + BOWMAN_SIZE, y + BOWMAN_SIZE / 2, controles));
        }
        /*
         if (simpleArrows.size() >= 3) {
         simpleArrows.add(new SimpleArrow(x + BOWMAN_SIZE, y + BOWMAN_SIZE / 2, controles));
         }
         */
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (controles == 0) {
            if (key == KeyEvent.VK_W) {
                dy = 0;
            }

            if (key == KeyEvent.VK_S) {
                dy = 0;
            }

            if (key == KeyEvent.VK_A) {
                dx = 0;
            }

            if (key == KeyEvent.VK_D) {
                dx = 0;
            }
        }
        if (controles == 1) {
            if (key == KeyEvent.VK_UP) {
                dy = 0;
            }

            if (key == KeyEvent.VK_DOWN) {
                dy = 0;

            }

            if (key == KeyEvent.VK_LEFT) {
                dx = 0;
            }

            if (key == KeyEvent.VK_RIGHT) {
                dx = 0;
            }
        }


    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }

    public void hit(Weapon w) {
        health -= w.getDamage();
    }

    public int getHealth() {
        return health;
    }

    public void removeProjectile(Weapon w) {
        projectiles.remove(w);

    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getMaxHealth() {
        return maxHealth;
    }
}
